<template>
  <div class="app-container">
    <el-dialog :title="dialogTitle" :visible.sync="dialogShow" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="任务名称" prop="jobDesc">
              <common-input v-model="form.jobDesc" placeholder="请输入任务名称"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务所属服务" prop="jobGroup">
              <el-select v-model="form.jobGroup" placeholder="请选择任务所属服务" clearable style="width: 100%">
                <el-option label="全部" value=""></el-option>
                <el-option v-for="executor in executorList" :label="executor.label"
                           :value="executor.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="方法名称" prop="executorHandler">
              <common-input v-model="form.executorHandler" placeholder="请输入方法名称"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="运行周期" prop="scheduleConf">
              <common-input v-model="form.scheduleConf" placeholder="请输入cron表达式"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="方法参数">
              <common-input type="textarea" :autosize="{ minRows: 4, maxRows: 8}" v-model="form.executorParam"
                        placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {task_add, task_update} from '@/api/taskcenter/all';

  export default {
    components: {},
    props: {
      executorList: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        dialogTitle: '新增', // 标题
        dialogShow: false, // 是否显示
        form: {},
        rules: {
          jobDesc: [
            {
              required: true,
              message: '请输入任务名称',
              trigger: 'blur',
            },
          ],
          jobGroup: [
            {
              required: true,
              message: '请选择任务所属服务',
            },
          ],
          executorHandler: [
            {
              required: true,
              message: '请输入方法名称',
              trigger: 'blur',
            },
          ],
          scheduleConf: [
            {
              required: true,
              message: '请输入cron表达式',
              trigger: 'blur',
            },
          ],
        },
      };
    },
    watch: {},
    computed: {},
    methods: {
      /**
       * 关闭
       */
      close() {
        this.$refs.form.resetFields();
        this.dialogShow = false;
        this.$emit('refresh');
      },
      /**
       * 显示
       */
      show(data) {
        this.dialogTitle = data.id ? '编辑' : '新增';
        this.dialogShow = true;
        this.form = data.id ? data : this.$options.data().form;
      },
      /**
       * 提交
       */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.form.id) {
              task_update(this.form).then((res) => {
                this.$GlobalSuccessMessage('操作成功');
                this.close();
              });
            } else {
              task_add(this.form).then((res) => {
                this.$GlobalSuccessMessage('操作成功');
                this.close();
              });
            }
          }
        });
      },
    },
    created() {
    },
    mounted() {
    },
  };
</script>
<style lang="scss" scoped>
</style>
